package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractDAO;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.SaleCostDAO;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.SaleCost;
import com.feeyin.radar.crm.service.AuditManageService;
import com.feeyin.radar.msg.dao.WarningInfoDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Users;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AuditManageServiceImpl
  implements AuditManageService
{
  private ContractDAO contractDAO;
  private SaleCostDAO saleCostDAO;
  private UserDAO userDAO;
  private CustomerDAO customerDAO;
  private DepartmentDAO departmentDAO;
  private WarningInfoDAO warningInfoDAO;

  public ContractDAO getContractDAO()
  {
    return this.contractDAO;
  }

  public void setContractDAO(ContractDAO contractDAO) {
    this.contractDAO = contractDAO;
  }

  public SaleCostDAO getSaleCostDAO() {
    return this.saleCostDAO;
  }

  public void setSaleCostDAO(SaleCostDAO saleCostDAO) {
    this.saleCostDAO = saleCostDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public WarningInfoDAO getWarningInfoDAO() {
    return this.warningInfoDAO;
  }

  public void setWarningInfoDAO(WarningInfoDAO warningInfoDAO) {
    this.warningInfoDAO = warningInfoDAO;
  }

  public List getPageAuditContracts(int sign, Integer userId, Page page) {
    if (page == null) return null;
    List contracts = new ArrayList();
    Contract contract = new Contract();
    if (sign == 1) {
      contract.setStatus("1");
    } else if (sign == 2) {
      if (userId == null) return null;
      Users user = new Users();
      user = this.userDAO.getUser(userId);
      if ((user == null) || (user.getDeptId() == null)) return null;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return null;

      contract.setDeptIds(deptIds);
      contract.setStatus("1");
    } else {
      contract.setStatus("1");
      contract.setApplyPersonId(userId);
    }

    contracts = this.contractDAO.getPageContractsByDynamic(contract, page);
    if (contracts == null) return null;
    for (int i = 0; i < contracts.size(); i++) {
      contract = new Contract();
      contract = (Contract)contracts.get(i);

      Customer customer = new Customer();
      customer = this.customerDAO.getCustomerById(contract.getCustomerId());
      if (customer != null) {
        contract.setCustomerName(customer.getCustomerName());
      }

      Users user = new Users();
      user = this.userDAO.getUser(contract.getSalePersonId());
      if (user != null) {
        contract.setSalePerson(user.getUserName());
      }
    }
    return contracts;
  }

  public Integer getPageAuditContractsCount(int sign, Integer userId) {
    List contracts = new ArrayList();
    Contract contract = new Contract();
    if (sign == 1) {
      contract.setStatus("1");
    } else if (sign == 2) {
      if (userId == null) return null;
      Users user = new Users();
      user = this.userDAO.getUser(userId);
      if (user == null) return null;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return null;

      contract.setDeptIds(deptIds);
      contract.setStatus("1");
    } else {
      contract.setStatus("1");
      contract.setApplyPersonId(userId);
    }
    return this.contractDAO.getPageContractsCountByDynamic(contract);
  }

  public boolean auditContract(String[] ids, Integer userId) {
    if ((ids == null) || (ids.length <= 0)) return false;
    if (userId == null) return false;
    for (int i = 0; i < ids.length; i++) {
      Contract contract = new Contract();
      contract.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      contract.setStatus("2");
      contract.setAuditPersonId(userId);
      contract.setAuditDate(new Date());
      boolean result = this.contractDAO.updateContractByDynamic(contract);

      if (!result)
        continue;
      contract = new Contract();
      contract = this.contractDAO.getContractById(Integer.valueOf(Integer.parseInt(ids[i])));
      Customer customer = new Customer();
      if ((contract != null) && (contract.getCustomerId() != null)) {
        customer = this.customerDAO.getCustomerById(contract.getCustomerId());
        if (customer != null) {
          Customer paraCustomer = new Customer();
          paraCustomer.setId(customer.getId());
          paraCustomer.setIsFollow("0");
          paraCustomer.setIsShare("0");
          paraCustomer.setCustomerCategory("3");
          paraCustomer.setUpdateSaleStageDate(new Date());
          BigDecimal contractCost = this.contractDAO.getContractCostsByCustomerIdByContractId(customer.getId(), contract.getId());
          if (contractCost == null)
            contractCost = contract.getContractCost();
          else {
            contractCost = contractCost.add(contract.getContractCost());
          }
          paraCustomer.setHistoryContractCost(contractCost);
          paraCustomer.setSaleStageId(Integer.valueOf(0));
          paraCustomer.setPredictCost(new BigDecimal(0));
          paraCustomer.setPredictContract(null);
          this.customerDAO.updateCustomer(paraCustomer);
        }

        this.warningInfoDAO.updateWarningInfoForProcessDate(Integer.valueOf(Integer.parseInt(ids[i])), "1", null);
      }
    }

    return true;
  }

  public boolean returnContract(String[] ids, Integer userId) {
    if ((ids == null) || (ids.length <= 0)) return false;
    if (userId == null) return false;
    for (int i = 0; i < ids.length; i++) {
      Contract contract = new Contract();
      contract.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      contract.setStatus("3");

      boolean result = this.contractDAO.updateContractByDynamic(contract);

      if (result) {
        this.warningInfoDAO.updateWarningInfoForProcessDate(Integer.valueOf(Integer.parseInt(ids[i])), "1", null);
      }
    }
    return true;
  }

  public List getPageAuditSaleCosts(int sign, Integer userId, Page page) {
    if (page == null) return null;
    List saleCosts = new ArrayList();
    SaleCost saleCost = new SaleCost();
    if (sign == 1) {
      saleCost.setStatus("1");
    } else if (sign == 2) {
      if (userId == null) return null;
      Users user = new Users();
      user = this.userDAO.getUser(userId);
      if (user == null) return null;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return null;

      saleCost.setDeptIds(deptIds);
      saleCost.setStatus("1");
    }

    saleCosts = this.saleCostDAO.getPageSaleCosts(saleCost, page);
    if (saleCosts == null) return null;
    for (int i = 0; i < saleCosts.size(); i++) {
      saleCost = new SaleCost();
      saleCost = (SaleCost)saleCosts.get(i);
      Users user = new Users();
      user = new Users();
      user = this.userDAO.getUser(saleCost.getApplyPersonId());
      if (user != null) {
        saleCost.setApplyPerson(user.getUserName());
      }
    }
    return saleCosts;
  }

  public Integer getPageAuditSaleCostsCount(int sign, Integer userId) {
    List saleCosts = new ArrayList();
    SaleCost saleCost = new SaleCost();
    if (sign == 1) {
      saleCost.setStatus("1");
    } else if (sign == 2) {
      if (userId == null) return null;
      Users user = new Users();
      user = this.userDAO.getUser(userId);
      if (user == null) return null;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return null;

      saleCost.setDeptIds(deptIds);
      saleCost.setStatus("1");
    }
    return this.saleCostDAO.getPageSaleCostsCount(saleCost);
  }

  public boolean auditSaleCost(String[] saleCostCodes, Integer userId) {
    if ((saleCostCodes == null) || (saleCostCodes.length <= 0)) return false;
    if (userId == null) return false;
    for (int i = 0; i < saleCostCodes.length; i++) {
      SaleCost saleCost = new SaleCost();
      saleCost.setSaleCostCode(saleCostCodes[i]);
      saleCost.setStatus("2");
      saleCost.setAuditPersonId(userId);
      saleCost.setAuditDate(new Date());
      boolean result = this.saleCostDAO.updateSaleCostBySaleCostCode(saleCost);
      if (!result)
        continue;
      this.warningInfoDAO.updateWarningInfoForProcessDate(null, "2", saleCostCodes[i]);
    }

    return true;
  }

  public boolean returnSaleCost(String[] saleCostCodes, Integer userId) {
    if ((saleCostCodes == null) || (saleCostCodes.length <= 0)) return false;
    if (userId == null) return false;
    for (int i = 0; i < saleCostCodes.length; i++) {
      SaleCost saleCost = new SaleCost();
      saleCost.setSaleCostCode(saleCostCodes[i]);
      saleCost.setStatus("3");
      saleCost.setAuditPersonId(userId);
      saleCost.setAuditDate(new Date());
      boolean result = this.saleCostDAO.updateSaleCostBySaleCostCode(saleCost);
      if (!result)
        continue;
      this.warningInfoDAO.updateWarningInfoForProcessDate(null, "2", saleCostCodes[i]);
    }

    return true;
  }
}